JAVA Gui on Hello World [closed]
Posted
by
user58892
on Programmers
See other posts from Programmers
or by user58892
Published on 2012-07-11T04:45:48Z
Indexed on
2012/07/11
9:21 UTC
Read the original article
Hit count: 235
java
I am designing, implementing, testing, and debuging a GUI-based version of a “Hello, World!” program in a JFrame that includes a JLabel that reads “Hello, World!” and I am trying to use a layout manager, and an Exit button to close the program.
Here's what I have so far, I would really apreciate if you could help on it syntax. I am 90% done but tried hard and it couldn't run.
import java.awt.*; // Needed for flow layout manager
import javax.swing.*; //All swing components live in the javax.swing package
import javax.swing.JButton; //to recognize buttons
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class HelloWorld {
public static void main(String[] args) {
//creates the label. The JLabel constructor
//takes an optional argument which set the text of the label
/* The text will be aligned with the center of the frame
* otherwise it will align on the left.
*/
JLabel label= new JLabel("Hello World!");
new FlowWindow();
label.setHorizontalAlignment (SwingConstants.CENTER);
JFrame frame = new JFrame("Hello");
//create exit button
JButton button1 = new JButton("Exit");
//Add exit button to the content pane.
add(button1);
frame.add(label);
frame.setSize(300, 300);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.toFront();
}
public static void FlowWindow()
{
//Add a new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
© Programmers or respective owner